home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Applications / MacDP 1.1.2 / MacDP.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-06  |  20.0 KB  |  1,006 lines  |  [TEXT/CWIE]

  1. /*
  2.  * MacDP2.c
  3.  *
  4.  * (c)1990-1994 SPDsoft <MacSPD@ivo.cps.unizar.es>
  5.  * Part of a batch VTR Frame-By-Frame recorder
  6.  * Version 1.1.1
  7.  *
  8.  * Grupo de Informatica Grafica, CPSUZ
  9.  *
  10.  */
  11.  
  12. #include <QuickTimeComponents.h>
  13. #include "MacDP.h"
  14.  
  15. /*
  16.  * Prototypes
  17.  */
  18.  
  19. void InitTheMac(void);
  20. void FlushTheMac(void);
  21. void CatString( Str255 StrIO, Str255 Str );
  22.  
  23. extern PicHandle
  24. ConvertFromJFIF(StringPtr name,short originalFile, CWindowPtr window);
  25. extern PicHandle
  26. ConvertFromPict(StringPtr name,short originalFile,CWindowPtr window);
  27.  
  28. /*
  29.  * Constants
  30.  */
  31.  
  32. #define keyOrMouse mDownMask+keyDownMask
  33. #define NIL 0L
  34.  
  35. #define CopyString(a,b)        BlockMove(a, b, *(a) + 1 )
  36.  
  37.  
  38. /*
  39.  * Globals
  40.  */
  41.  
  42. TPrint        myTPrint;
  43. TPPrint        myTPPrint = &myTPrint;
  44. THPrint        ghPtrRec = &myTPPrint;
  45.  
  46. WindowPtr     gMyWindow = nil;
  47. Str63        gTheName = "\p File";
  48. Rect        PackORects[7];
  49. char        LastMonitorID=0;
  50. int            theMonitorID;
  51. Boolean        CanOpenJFIF = false;
  52.  
  53. static short    oldMBarHeight;
  54. static int        oldTop;
  55.  
  56. Boolean        quitting = false;
  57. #define        update                ( gMyWindow != nil )
  58. Boolean        HiddenMenu=false;
  59. Boolean        BlackBack=true;
  60.  
  61. MenuHandle            myMenus[4];
  62. static PicHandle    thePic=nil;
  63. HFileInfo    gpb;
  64.  
  65. /*
  66.  * AE
  67.  */
  68. Boolean AppleEventsInstalled (void)
  69. {
  70.     OSErr err;
  71.     long  result;
  72.  
  73.     err = Gestalt (gestaltAppleEventsAttr, &result);
  74.     return (!err && ((result >> gestaltAppleEventsPresent) & 0x0001));
  75. }
  76.  
  77. OSErr MyGotRequiredParams (AppleEvent *theAppleEvent)
  78. {
  79.     DescType    returnedType;
  80.     Size    actualSize;
  81.     OSErr    err;
  82.  
  83.     err = AEGetAttributePtr (theAppleEvent, keyMissedKeywordAttr,
  84.                                     typeWildCard, &returnedType, nil, 0,
  85.                                     &actualSize);
  86.     if (err == errAEDescNotFound)    // you got all the required parameters
  87.             return noErr;
  88.     else if (!err)                // you missed a required parameter
  89.             return errAEEventNotHandled;
  90.     else                        // the call to AEGetAttributePtr failed
  91.             return err;
  92. }
  93. /******************************************************************************/
  94.  
  95.  
  96. pascal OSErr  MyHandleODoc (AppleEvent *theAppleEvent, AppleEvent* reply, long
  97.                                                         handlerRefCon)
  98. {
  99.     FSSpec    myFSS;
  100.     AEDescList    docList;
  101.     OSErr    err;
  102.     long    index,
  103.             itemsInList;
  104.     Size    actualSize;
  105.     AEKeyword    keywd;
  106.     DescType    returnedType;
  107.  
  108.  
  109.     // get the direct parameter--a descriptor list--and put it into a docList
  110.     err = AEGetParamDesc (theAppleEvent, keyDirectObject, typeAEList,
  111.             &docList);
  112.     if (err)
  113.             return err;
  114.  
  115.     
  116.     // check for missing parameters
  117.     err = MyGotRequiredParams (theAppleEvent);
  118.     if (err)
  119.             return err;
  120.  
  121.     
  122.     // count the number of descriptor records in the list
  123.     err = AECountItems (&docList, &itemsInList);
  124.  
  125.     // now get each descriptor record from the list, coerce the returned
  126.     // data to an FSSpec record, and open the associated file
  127.  
  128.     for (index = 1; index <= itemsInList; index++) {
  129.  
  130.             err = AEGetNthPtr (&docList, index, typeFSS, &keywd,
  131.                     &returnedType, (Ptr) &myFSS, sizeof(myFSS), &actualSize);
  132.             if (err)
  133.                     return err;
  134.  
  135.             
  136.     }
  137.     DoOpen(&myFSS);
  138.     
  139.     err = AEDisposeDesc (&docList);
  140.     return noErr;
  141. }
  142.  
  143. pascal OSErr  MyHandlePDoc (AppleEvent *theAppleEvent, AppleEvent *reply, long
  144.                                                         handlerRefCon)
  145. {
  146.     FSSpec    myFSS;
  147.     AEDescList    docList;
  148.     OSErr    err;
  149.     long    index,
  150.             itemsInList;
  151.     Size    actualSize;
  152.     AEKeyword    keywd;
  153.     DescType    returnedType;
  154.  
  155.  
  156.     // get the direct parameter--a descriptor list--and put it into a docList
  157.     err = AEGetParamDesc (theAppleEvent, keyDirectObject, typeAEList,
  158.             &docList);
  159.     if (err)
  160.             return err;
  161.  
  162.     // check for missing parameters
  163.     err = MyGotRequiredParams (theAppleEvent);
  164.     if (err)
  165.             return err;
  166.  
  167.     // count the number of descriptor records in the list
  168.     err = AECountItems (&docList, &itemsInList);
  169.  
  170.     // now get each descriptor record from the list, coerce the returned
  171.     // data to an FSSpec record, and open the associated file
  172.  
  173.  
  174.  
  175.     for (index = 1; index <= itemsInList; index++) {
  176.  
  177.             err = AEGetNthPtr (&docList, index, typeFSS, &keywd,
  178.                     &returnedType, (Ptr) &myFSS, sizeof(myFSS), &actualSize);
  179.             if (err)
  180.                     return err;
  181.             
  182.             DoOpen(&myFSS);
  183.             UpdateWindow(gMyWindow);
  184.             DoFile(fmPrint);
  185.             
  186.     }
  187.     
  188.     err = AEDisposeDesc (&docList);
  189.     return noErr;
  190. }
  191.  
  192. pascal OSErr  MyHandleOApp (AppleEvent *theAppleEvent, AppleEvent *reply, long
  193.                                                         handlerRefCon)
  194. {
  195.     OSErr    err;
  196.  
  197.     if ( err = MyGotRequiredParams (theAppleEvent))
  198.             return err;
  199.             
  200.     return ( noErr );
  201. }
  202.  
  203. pascal    OSErr    MyHandleQuit (AppleEvent *theAppleEvent, AppleEvent *reply,
  204.                     long handlerRefcon)
  205. {
  206.     OSErr    err;
  207.     extern Boolean quitting;
  208.     
  209.     if (err = MyGotRequiredParams(theAppleEvent)) {
  210.         // an error occurred:  do the necessary error handling
  211.         return    err;
  212.     }
  213.     
  214.     
  215.     DoFile(fmQuit);
  216.     
  217.     return ( noErr );
  218. }
  219. /******************************************************************************/
  220.  
  221.  
  222. void InitTheMac(void)
  223. {
  224.     SysEnvRec    theWorld;
  225.     GDHandle    GDHdl;
  226.     long        resp;
  227.  
  228.     MaxApplZone();
  229.     FlushEvents(everyEvent, 0);
  230.     InitGraf(&qd.thePort);
  231.     InitFonts();
  232.     InitWindows();
  233.     InitMenus();
  234.     TEInit();
  235.     InitDialogs(NIL);
  236.     InitCursor();
  237.  
  238.     oldMBarHeight=LMGetMBarHeight();
  239.     
  240.     if (SysEnvirons(curSysEnvVers, &theWorld) != noErr)
  241.     {
  242.         ParamText("\pSorry, SysEnvirons failed","\p(System too old?)","\p","\p");
  243.         StopAlert(WarningDlg ,NULL);
  244.         ExitToShell();
  245.     }
  246.     
  247.     if(theWorld.hasColorQD)
  248.     {
  249.         GDHdl = GetDeviceList();
  250.         
  251.         do
  252.         {
  253.             BlockMove(&(**GDHdl).gdRect, &PackORects[LastMonitorID], sizeof(Rect));
  254.             LastMonitorID++;
  255.         }while((GDHdl = GetNextDevice(GDHdl))!=NIL);
  256.     }
  257.     else
  258.     {
  259.         ParamText("\pSorry, Color QuickDraw not found","\p(System too old?)","\p","\p");
  260.         StopAlert(WarningDlg ,NULL);
  261.         ExitToShell();
  262.  
  263.     }
  264.     if (AppleEventsInstalled())
  265.     {
  266.         AEInstallEventHandler (kCoreEventClass, kAEOpenDocuments,
  267.                                                     (AEEventHandlerUPP)MyHandleODoc,0, FALSE);
  268.         AEInstallEventHandler (kCoreEventClass, kAEOpenApplication,
  269.                                                     (AEEventHandlerUPP)MyHandleOApp,0, FALSE);
  270.         AEInstallEventHandler (kCoreEventClass, kAEPrintDocuments,
  271.                                                     (AEEventHandlerUPP)MyHandlePDoc,0, FALSE);
  272.         AEInstallEventHandler (kCoreEventClass, kAEQuitApplication,
  273.                                                     (AEEventHandlerUPP)MyHandleQuit,0, FALSE);
  274.     }
  275.     else
  276.     {
  277.         ParamText("\pSorry, AppleEvents not found","\p(System too old?)","\p","\p");
  278.         StopAlert(WarningDlg ,NULL);
  279.         ExitToShell();
  280.     }
  281.     
  282.     theMonitorID=LastMonitorID;
  283.     thePic=nil;
  284.     CanOpenJFIF =  !( Gestalt(gestaltCompressionMgr, &resp) != noErr || resp < 15 );
  285.  
  286.     PrOpen();
  287.         PrintDefault(ghPtrRec);
  288.     PrClose();
  289.     
  290. #ifdef SHRINKWINDOWFORDEBUG
  291.     PackORects[0].top+= 100;
  292.     PackORects[0].bottom-= 100;
  293. #endif    
  294. }
  295.  
  296. void FlushTheMac(void)
  297. {
  298.  
  299.     LMSetMBarHeight(oldMBarHeight);
  300. }
  301.  
  302. void main( void )
  303.  
  304. { /* main program */
  305.     InitTheMac();
  306.     SetUpMenus();
  307.     
  308.     while( !quitting )
  309.         MainEvent();
  310.  
  311.     FlushTheMac();
  312. }
  313.  
  314.  
  315. void MainEvent(void)
  316. {
  317.     EventRecord    myEvent;
  318.     WindowPtr    whichWindow;
  319.     short        windowPart;
  320.     Rect        tmpRect;
  321.  
  322.     SystemTask();
  323.     if( GetNextEvent(everyEvent, &myEvent) ) {
  324.         switch( myEvent.what ) {
  325.             case mouseDown:
  326.                 windowPart = FindWindow(myEvent.where, &whichWindow);
  327.                 DoMouseDown(windowPart, whichWindow, &myEvent);
  328.                 break;
  329.                 
  330.             case keyDown:
  331.             case autoKey: 
  332.             {
  333.                 register char theChar;
  334.     
  335.                 theChar = myEvent.message & charCodeMask;
  336.                 if ((myEvent.modifiers & cmdKey) != 0) 
  337.                     DoCommand( MenuKey(theChar) );
  338.                 else
  339.                     if ((theChar=='\t')&&(update))
  340.                     {
  341.                         if (HiddenMenu)
  342.                         {
  343.                             (**(*gMyWindow).visRgn).rgnBBox.top=oldTop;
  344.                             
  345.                             SetRect(&tmpRect,
  346.                                 qd.thePort->portRect.left,
  347.                                 qd.thePort->portRect.top,
  348.                                 qd.thePort->portRect.right,
  349.                                 qd.thePort->portRect.top + oldMBarHeight
  350.                             );
  351.                             
  352.                             InvalRect( &tmpRect );
  353.  
  354.                             HiddenMenu=false;
  355.                             LMSetMBarHeight(oldMBarHeight);
  356.                             ShowCursor();
  357.                             DrawMenuBar();
  358.  
  359.                         }
  360.                         else
  361.                         {
  362.                             oldTop=(**(*gMyWindow).visRgn).rgnBBox.top;
  363.                             RectRgn( (*gMyWindow).visRgn, &(*qd.thePort).portRect );
  364.                             
  365.                             
  366.                             SetRect(&tmpRect,
  367.                                 qd.thePort->portRect.left,
  368.                                 qd.thePort->portRect.top,
  369.                                 qd.thePort->portRect.right,
  370.                                 qd.thePort->portRect.top + oldMBarHeight
  371.                             );
  372.                             
  373.                             InvalRect( &tmpRect );
  374.                             
  375.                             LMSetMBarHeight(0);
  376.                             HiddenMenu=true;
  377.                             HideCursor();
  378.  
  379.                         }
  380.                     }
  381.                     else if ((theChar==' ')&&(update))
  382.                     {
  383.                         if ( BlackBack=!BlackBack )
  384.                                 BackPat(&qd.black);
  385.                         else
  386.                                 BackPat(&qd.white);
  387.  
  388.                         InvalRect( &(*qd.thePort).portRect );
  389.                     }
  390.                     
  391.                 break;
  392.             }
  393.             case osEvt:
  394.                 switch ((myEvent.message >> 24) & 0x0ff)
  395.                 {
  396.                     case suspendResumeMessage:
  397.                         
  398.                         SetCursor(&qd.arrow);
  399.                     
  400.                         if ((myEvent.message & resumeFlag) == 0) // suspend 
  401.                         {
  402.                             if (HiddenMenu)
  403.                             {
  404.                                 (**(*gMyWindow).visRgn).rgnBBox.top=oldTop;
  405.                                 LMSetMBarHeight(oldMBarHeight);
  406.                                 ShowCursor();
  407.                                 DrawMenuBar();
  408.     
  409.                             }
  410.                         }
  411.                         else
  412.                         {                                        // resume
  413.                             if (HiddenMenu)
  414.                             {
  415.                                 oldTop=(**(*gMyWindow).visRgn).rgnBBox.top;
  416.                                 RectRgn( (*gMyWindow).visRgn, &(*qd.thePort).portRect );
  417.                                 InvalRect( &(*qd.thePort).portRect );
  418.                                 LMSetMBarHeight(0);
  419.                                 HideCursor();
  420.                             }
  421.                         }
  422.                         break;
  423.                         
  424.                     case mouseMovedMessage:
  425.                         break;
  426.                 }
  427.                 break;
  428.             case activateEvt:
  429.                 break;
  430.  
  431.             case updateEvt: 
  432.                 UpdateWindow((WindowPtr)myEvent.message);
  433.                 break;
  434.  
  435.             case kHighLevelEvent:
  436.                 AEProcessAppleEvent (&myEvent);
  437.                 break;
  438.  
  439.  
  440.         }
  441.     }
  442. }
  443. void UpdateWindow(WindowPtr theWindow)
  444. {
  445.     GrafPtr savePort;
  446.     Rect    destRect;    
  447.     short    pw, ph, ww, wh, dw, dh;
  448.     
  449.     GetPort(&savePort);
  450.     SetPort(theWindow);
  451.  
  452.     BeginUpdate(theWindow);
  453.     DrawControls(theWindow);
  454.  
  455.     EraseRect(&theWindow->portRect);
  456.  
  457.     if( thePic != nil )
  458.     {
  459.         BlockMove(&(*thePic)->picFrame, &destRect, sizeof(Rect));
  460.         ww = theWindow->portRect.right - theWindow->portRect.left;
  461.         wh = theWindow->portRect.bottom - theWindow->portRect.top;
  462.         pw = destRect.right - destRect.left;
  463.         ph = destRect.bottom - destRect.top;
  464.         
  465.         dw = (ww - pw)/2;
  466.         dh = (wh - ph)/2;
  467.                 
  468.         OffsetRect( &destRect, dw, dh );
  469.  
  470.         if (( dw < dh ) && ( dw < 0 ))
  471.             InsetRect( &destRect, -dw, -dw * (float) ph / (float) pw );
  472.         else if (( dh < dw ) && ( dh < 0 ))
  473.             InsetRect( &destRect, -dh * (float) pw / (float) ph, -dh );
  474.             
  475.         DrawPicture( thePic, &destRect);
  476.     }
  477.     
  478.     EndUpdate(theWindow);
  479.  
  480.     SetPort(savePort);
  481. }
  482.  
  483. void SetUpMenus(void)
  484. {
  485.     short index;
  486.     Str255 stmp, snum;
  487.  
  488.     myMenus[appleM] = GetMenu(appleID);
  489.     AddResMenu(myMenus[appleM], 'DRVR');
  490.     myMenus[fileM] = GetMenu(fileID);
  491.     myMenus[editM] = GetMenu(editID);
  492.     myMenus[monitorM] = GetMenu(monitorID);
  493.  
  494.     
  495.     for( index=(short)LastMonitorID-1;index>=0;index--)
  496.     {
  497.         *stmp=0x00;
  498.         CatString(stmp,"\pSlot ");
  499.         NumToString((long)(LastMonitorID-index),snum);
  500.         CatString(stmp,snum);
  501.         CatString(stmp,"\p/");
  502.         CatString(stmp,snum);
  503.         AppendMenu(myMenus[monitorM],stmp);
  504.     }
  505.     
  506.  
  507.     for(index=appleM; index <= monitorM; index++)
  508.         InsertMenu(myMenus[index], 0) ;
  509.     CheckItem( myMenus[monitorM], theMonitorID, true);
  510.     
  511.     DisableItem(myMenus[editM],emUndo);
  512.     DisableItem(myMenus[editM],emClear);
  513.     DisableItem(myMenus[editM],emCut);
  514.     DisableItem(myMenus[editM],emPaste);
  515.     
  516.     DisableItem(myMenus[editM],emCopy);
  517.     
  518.     DrawMenuBar();
  519. }
  520.  
  521. void DoCommand(long mResult)
  522. {
  523.     short    theItem;
  524.     Str255    name;
  525.  
  526.     theItem = LoWord(mResult);
  527.     switch( HiWord(mResult) ) {
  528.         case appleID:
  529.             if( theItem > 2 ) {
  530.                 GrafPtr savePort;
  531.                 GetItem(myMenus[appleM], theItem, name);
  532.                 GetPort(&savePort);
  533.                 OpenDeskAcc(name);
  534.                 SetPort(savePort);
  535.             }
  536.             else
  537.             {
  538.                 ParamText("\p©SPDsoft 94: MacDP 1.1.1",
  539.                 "\p<Tab>=Full Screen; <Spc>=B/W",
  540.                 "\pe-mail: MacSPD@ivo.cps.unizar.es","\p");
  541.                 NoteAlert(AboutDlg,NULL);
  542.             }
  543.  
  544.             break;
  545.  
  546.         case fileID:
  547.             DoFile(theItem);
  548.             break;
  549.  
  550.         case editID:
  551.         
  552.             if( SystemEdit(theItem-1) == FALSE )
  553.             {
  554.                 if (( theItem == emCopy ) && (thePic != nil ))
  555.                 {
  556.                     ZeroScrap();
  557.                     HLock((Handle)thePic);
  558.                     PutScrap(
  559.                         (long) GetHandleSize((Handle)thePic),
  560.                         (ResType)'PICT',
  561.                         (Ptr) *thePic
  562.                     );
  563.                     HUnlock((Handle)thePic);
  564.                 }
  565.             }
  566.             break;
  567.             
  568.         case monitorID:
  569.         
  570.             CheckItem( myMenus[monitorM], theMonitorID, false);
  571.             theMonitorID=( (theItem>LastMonitorID)?LastMonitorID:theItem);
  572.             CheckItem( myMenus[monitorM], theMonitorID, true);
  573.             
  574.             
  575.             if(gMyWindow!=nil)
  576.             {
  577.             MoveWindow(gMyWindow,
  578.                 PackORects[theMonitorID-1].left,
  579.                 PackORects[theMonitorID-1].top,
  580.                 TRUE);
  581.             SizeWindow(gMyWindow,
  582.                 PackORects[theMonitorID-1].right - PackORects[theMonitorID-1].left,
  583.                 PackORects[theMonitorID-1].bottom - PackORects[theMonitorID-1].top,
  584.                 TRUE);
  585.             InvalRect( &(*qd.thePort).portRect );
  586.             }
  587. /*            
  588.             if (HiddenMenu)
  589.             {
  590.                 MBarHeight=oldMBarHeight;
  591.                 DrawMenuBar();
  592.                 (**(*gMyWindow).visRgn).rgnBBox.top=oldTop;
  593.                 InvalRect( &(*qd.thePort).portRect );
  594.  
  595.                 HiddenMenu=false;
  596.                 MBarHeight=oldMBarHeight;
  597.                 ShowCursor();
  598.                 DrawMenuBar();
  599.                 
  600.                 oldTop=(**(*gMyWindow).visRgn).rgnBBox.top;
  601.                 RectRgn( (*gMyWindow).visRgn, &(*qd.thePort).portRect );
  602.                 InvalRect( &(*qd.thePort).portRect );
  603.                 MBarHeight=0;
  604.                 HiddenMenu=true;
  605.                 HideCursor();
  606.             }
  607. */                
  608.             break;
  609.     }
  610.  
  611.     HiliteMenu(0);
  612. }
  613.  
  614. void DoFile(short item)
  615. {
  616.     OSErr                result;
  617.     short compressedFile = 0;
  618.     SFReply        sfr;    
  619.     Point        pt = {100,100};
  620.     char        zero=0;
  621.     long        l;
  622.     short        i;
  623.     
  624.     switch( item ) {
  625.  
  626.         case fmOpen:
  627.         
  628.             DoOpen((FSSpec *)nil);
  629.             break;
  630.  
  631.         case fmClose:
  632.  
  633.             if (update) DoClose();
  634.             DisableItem(myMenus[fileM],fmClose);
  635.             break;
  636.  
  637.         case fmSaveAs:
  638.             
  639.             /*
  640.              * code for 'fmSaveAs' is from
  641.              * JPEGtoPICT.c, Written by:    Mark Krueger, Apple Computer, Inc.
  642.              *
  643.              */
  644.              
  645.             if (thePic!=nil)
  646.             {
  647.                 SFPutFile(pt,"\pSave PICT:",gTheName,NULL,&sfr);
  648.                 if ( !sfr.good )  {
  649.                     goto done;
  650.                 }
  651.                 
  652.                 /************************************************
  653.                  *
  654.                  *    Create the new file, if we can.
  655.                  *
  656.                  ************************************************/
  657.                 
  658.                 FSDelete(sfr.fName,sfr.vRefNum);
  659.                 if ( (result=Create(sfr.fName,sfr.vRefNum,'MCDJ','PICT')) != noErr ) {
  660.                     goto done;
  661.                 }
  662.                 if ( (result=FSOpen(sfr.fName,sfr.vRefNum,&compressedFile)) != noErr ) {
  663.                     goto done;
  664.                 }
  665.         
  666.                 /************************************************
  667.                  *
  668.                  * write the silly 512 bytes of zero needed at the start of a PICT file
  669.                  *
  670.                  ************************************************/
  671.                     
  672.                 SetFPos(compressedFile,fsFromStart,512);
  673.                 l = 1;
  674.                 for ( i=0; i < 512; i++) {
  675.                     if ( (result=FSWrite(compressedFile,&l,&zero)) )
  676.                         break;
  677.                 }
  678.     
  679.                 /************************************************
  680.                  *
  681.                  * write the QuickTime picture data.
  682.                  *
  683.                  ************************************************/
  684.                 
  685.                 l = GetHandleSize((Handle)thePic);
  686.                     
  687.                 HLock((Handle)thePic);
  688.                 result=FSWrite(compressedFile,&l,*thePic);
  689.                 HUnlock((Handle)thePic);
  690.                 
  691. done:    
  692.                 if ( compressedFile )
  693.                 {
  694.                     FSClose(compressedFile);
  695.                     if ( result != noErr )
  696.                         FSDelete(sfr.fName,sfr.vRefNum);
  697.                     FlushVol(nil,sfr.vRefNum);
  698.                 }
  699.  
  700.             }
  701.             break;
  702.  
  703.         case fmPageSetUp:
  704.         
  705.                 PrOpen();
  706.                     PrintDefault(ghPtrRec);
  707.                     PrStlDialog(ghPtrRec);
  708.                 PrClose();
  709.  
  710.             break;
  711.  
  712.         case fmPrint:
  713.             {
  714.                 TPPrPort    myPrtPort;
  715.                 Rect        destRect;
  716.                 TPrStatus    myPrStatus;
  717.                 GrafPtr        savePort;
  718.  
  719.                 
  720.                 if( thePic == nil )
  721.                     break;
  722.                     
  723.                 GetPort(&savePort);
  724.                 
  725.                 PrOpen();
  726.                 if(PrJobDialog(ghPtrRec))
  727.                 {
  728.                     
  729.                     myPrtPort = PrOpenDoc( ghPtrRec, 0,0 );
  730.                     PrOpenPage( myPrtPort , 0 );
  731.                     if ( PrError() )
  732.                         goto done_pr;
  733.                     
  734.                             
  735.  
  736.                     if( thePic != nil )
  737.                     {
  738.                         BlockMove(&(*thePic)->picFrame, &destRect, sizeof(Rect));
  739.         
  740.                         OffsetRect(
  741.                             &destRect,
  742.                 
  743.                             ((myPrtPort->gPort.portRect.right-myPrtPort->gPort.portRect.left) -
  744.                             (destRect.right - destRect.left)) / 2
  745.                             ,
  746.                             
  747.                             ((myPrtPort->gPort.portRect.bottom-myPrtPort->gPort.portRect.top) -
  748.                             (destRect.bottom - destRect.top)) / 2
  749.                             );
  750.  
  751.                         DrawPicture( thePic, &destRect);
  752.                     }
  753.                     
  754.                     PrClosePage( myPrtPort );
  755.                     
  756.                     PrCloseDoc( myPrtPort );
  757.                     
  758.                     if ( (*ghPtrRec)->prJob.bJDocLoop == bSpoolLoop )
  759.                     {
  760.                         PrPicFile( ghPtrRec, 0,0,0, &myPrStatus );
  761.                     }
  762.                 }
  763. done_pr:
  764.                 
  765.                 PrClose();
  766.                 SetPort(savePort);
  767.             }
  768.             break;
  769.  
  770.  
  771.         case fmQuit:
  772.         
  773.             DoFile(fmClose);
  774.             quitting = true;
  775.             break;
  776.     }
  777. }
  778.  
  779. void DoMouseDown(short windowPart, WindowPtr whichWindow, EventRecord *myEvent)
  780. {
  781.     switch( windowPart ) {
  782.         case inGoAway:
  783.             if( TrackGoAway(whichWindow, myEvent->where) )
  784.                 DoFile(fmClose);
  785.             break;
  786.  
  787.         case inMenuBar:
  788.             DoCommand( MenuSelect(myEvent->where) );
  789.             break;
  790.  
  791.         case inSysWindow:
  792.             SystemClick(myEvent, whichWindow);
  793.             break;
  794.  
  795.         case inDrag:
  796.             break;
  797.  
  798.         case inGrow:
  799.             break;
  800.  
  801.         case inContent:
  802.             if( whichWindow != FrontWindow() )
  803.                 SelectWindow(whichWindow);
  804.             break;
  805.     }
  806. }
  807. /********************************************************************************/
  808.  
  809. void    DoClose(void)
  810. {
  811.     if (HiddenMenu)
  812.     {
  813.         HiddenMenu=false;
  814.         LMSetMBarHeight(oldMBarHeight);
  815.         ShowCursor();
  816.         DrawMenuBar();
  817.     }
  818.  
  819.     if (( gMyWindow == FrontWindow() ) && ( gMyWindow != nil ))
  820.     {
  821.         DisposeWindow(gMyWindow);
  822.         gMyWindow = nil;
  823.     }
  824.     
  825.     if (thePic)
  826.     {
  827.         KillPicture(thePic);
  828.         thePic = nil;
  829.     }
  830.     DisableItem(myMenus[editM],emCopy);
  831.     DisableItem(myMenus[fileM],fmSaveAs);
  832.     DisableItem(myMenus[fileM],fmPrint);
  833. }
  834.  
  835. void    DoOpen( FSSpec *myFSSpec )
  836. {
  837.     StandardFileReply    mySFR;
  838.     HFileInfo            pb;
  839.     OSErr                error;
  840.     
  841.     SFTypeList        typeList={ 'PICT','JFIF','JPEG','\?\?\?\?' };
  842.  
  843.  
  844.  
  845.     if ( myFSSpec == nil )
  846.     {
  847.         if(CanOpenJFIF)
  848.         {
  849.             StandardGetFilePreview( nil, 3, typeList, &mySFR);
  850.         }
  851.         else
  852.         {    
  853.             StandardGetFile( nil, 1, typeList, &mySFR);
  854.         }
  855.  
  856.  
  857.         if( !mySFR.sfGood ) return;
  858.         
  859.         myFSSpec = &(mySFR.sfFile);
  860.     }
  861.         
  862.  
  863. #ifdef DO_FOLDERS
  864.     pb.ioNamePtr    = myFSSpec->name;
  865.     pb.ioVRefNum    = myFSSpec->vRefNum;
  866.     pb.ioFDirIndex    = 0;                    /* query 1 item */
  867.     pb.ioDirID        = myFSSpec->parID;
  868.     
  869.     PBGetCatInfo( (CInfoPBPtr)&pb, 0 );
  870.     if (((pb.ioFlAttrib>>4) & 0x01) == 1)
  871.     {
  872.         /* Is a Dir */
  873.         gpb.ioNamePtr = myFSSpec->name;
  874.         gpb.ioVRefNum = myFSSpec->vRefNum;
  875.  
  876.         RecurseDir(pb.ioDirID);
  877.     }
  878.     else
  879.     {
  880. #endif
  881.         /* Is a File */
  882.         
  883.         OpenFile( myFSSpec );
  884.     
  885. #ifdef DO_FOLDERS
  886.     }
  887. #endif
  888.  
  889. }
  890.  
  891. void    OpenFile( FSSpec *myFSSpec )
  892. {
  893.     short            fRefNum;
  894.     FInfo            finfo;
  895.  
  896.  
  897.     if (thePic)
  898.         KillPicture(thePic);
  899.         
  900.     thePic = nil;
  901.     
  902.     if ( FSpOpenDF(myFSSpec, fsCurPerm, &fRefNum) == noErr )
  903.     {
  904.         if ( gMyWindow == nil)
  905.         {
  906.             gMyWindow = NewCWindow(
  907.                             nil, &PackORects[theMonitorID-1],
  908.                             "\p", TRUE, 2, (WindowPtr)(-1L), FALSE, 0L
  909.                         );
  910.         }
  911.         else
  912.         {
  913.             InvalRect( &(*qd.thePort).portRect );
  914.         }
  915.  
  916.         if ( gMyWindow == nil)
  917.         {
  918.             SysBeep(1);
  919.             ExitToShell();
  920.         }
  921.     
  922.         SetPort(gMyWindow);
  923.         
  924.         PenPat(&qd.white);
  925.         BackPat(BlackBack ? &qd.black:&qd.white);
  926.     
  927.         
  928.         FSpGetFInfo(myFSSpec, &finfo);
  929.         
  930.         if ( finfo.fdType == 'PICT' )
  931.         {
  932.             thePic=ConvertFromPict(myFSSpec->name, fRefNum, (CWindowPtr)gMyWindow);
  933.         }
  934.         else if ( ( finfo.fdType == 'JPEG' ) || ( finfo.fdType == 'JFIF' ))
  935.         {
  936.             if(CanOpenJFIF)
  937.             thePic=ConvertFromJFIF(myFSSpec->name, fRefNum, (CWindowPtr)gMyWindow);
  938.         }
  939.     }
  940.     
  941.     if (thePic==nil)
  942.     {
  943.         DoClose();
  944.     }
  945.     else
  946.     {    
  947.         CopyString( myFSSpec->name, gTheName );
  948.         EnableItem(myMenus[fileM],fmClose);
  949.         EnableItem(myMenus[editM],emCopy);
  950.         EnableItem(myMenus[fileM],fmSaveAs);
  951.         EnableItem(myMenus[fileM],fmPrint);
  952.     }
  953. }
  954.  
  955.  
  956. void RecurseDir(long dirIDToSearch)
  957. {
  958. #ifdef DO_FOLDERS
  959.     short int    index=1;      /* for ioFDirIndex */
  960.     OSErr        err;          /* the usual */
  961.     FSSpec        myFSSpec;
  962.  
  963.     do
  964.     {
  965.          gpb.ioDirID        = dirIDToSearch;     
  966.          gpb.ioFDirIndex    = index;        /* set up the index */
  967.                                             /* we need to do this every time through,
  968.                                              * since GetCatInfo returns ioFlNum
  969.                                              * in this field
  970.                                              */
  971.          err= PBGetCatInfo((CInfoPBPtr)&gpb,false);
  972.  
  973.          if (err == noErr) 
  974.          {
  975.              /* check the file attributes for folderhood */
  976.              if (((gpb.ioFlAttrib>>4) & 0x01) == 1)
  977.              {
  978.                   RecurseDir(gpb.ioDirID);
  979.                   err = 0;
  980.              }
  981.              else 
  982.              {
  983.                      FSMakeFSSpec(
  984.                          0,
  985.                          dirIDToSearch,
  986.                          gpb.ioNamePtr,
  987.                          &myFSSpec
  988.                          );
  989.                          
  990.                     OpenFile( &myFSSpec );
  991.             
  992.              }
  993.              
  994.         index += 1;    /* increment the index for GetCatInfo */
  995.         }
  996.     } while ((err == noErr)&&(!Button()));
  997. #endif    
  998. }
  999.  
  1000. void CatString( Str255 StrIO, Str255 Str )
  1001. {
  1002.     BlockMove(&Str[1], (Str255 *)(StrIO+(int)*StrIO+1), (int) *Str);
  1003.     (*StrIO) += (unsigned int)*Str;
  1004. }
  1005.  
  1006.